package com.info.dpunified.dpgen.service;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.info.dpunified.common.exception.DPUnifiedException;
import com.info.dpunified.dpgen.dataaccess.RadarDefectDAO;

public class DefectUploadService 
{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private RadarDefectDAO radarDefectDAO;
	private DefectExtractor defectExtrator;
	
	/**
	 * @return the defectExtrator
	 */
	public DefectExtractor getDefectExtrator() {
		return defectExtrator;
	}

	/**
	 * @param defectExtrator the defectExtrator to set
	 */
	public void setDefectExtrator(DefectExtractor defectExtrator) {
		this.defectExtrator = defectExtrator;
	}

	/**
	 * @return the radarDefectDAO
	 */
	public RadarDefectDAO getRadarDefectDAO() {
		return radarDefectDAO;
	}

	/**
	 * @param radarDefectDAO the radarDefectDAO to set
	 */
	public void setRadarDefectDAO(RadarDefectDAO radarDefectDAO) {
		this.radarDefectDAO = radarDefectDAO;
	}
	
	
	public void insertDefects(InputStream fileContents, String fileName) throws DPUnifiedException
	{
		logger.info("Uploading defects from file "+fileName);
		this.getRadarDefectDAO().insertDefects(this.getDefectExtrator().extractDefects(fileContents,fileName));
	}

	public void insertDefects(String filePath) throws DPUnifiedException
	{
		logger.info("Uploading defects from path "+filePath);
		this.getRadarDefectDAO().insertDefects(this.getDefectExtrator().extractDefects(filePath));
	}
	
	public static void main(String args[]) throws DPUnifiedException
	{
		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-context.xml")) ;
		DefectUploadService defectUploadService =(DefectUploadService) beanFactory.getBean("defectUploadService");
		defectUploadService.insertDefects(null);
	}

}
